﻿/*
 * Copyright (c) United Binary LLC.  All rights reserved.
 * 
 * This code is licensed under the MIT License
 * 
 * SEE: http://harnessit.codeplex.com/license
 * 
 */
using System;
using System.Windows.Forms;
using UnitedBinary.UnitTests.Framework;
using UnitedBinary.HarnessIt.SharedTypes;
using UnitedBinary.Core.Windows.Forms;
using UnitedBinary.HarnessIt.Forms;
using UnitedBinary.UnitTests.Framework.TestItems;

namespace UnitedBinary.HarnessIt.Controls
{
	public partial class TestResultsControl : UserControl
	{
		RichTextComposition rtCompReport = new RichTextComposition();
		public IMethodLookup Lookup { get; set; }
		public TestResultsControl()
		{
			InitializeComponent();
			rtCompReport.RichTextBox = richTextBoxDetails;
			rtCompReport.OwnsRichTextBox = true;
		}

		internal void AddTestResult(IMethod iMethod)
		{
			testStatusControl.AddTestResult(iMethod);
		}

		internal void RenderResults()
		{
			testStatusControl.RenderResults();
		}

		internal void Clear()
		{
			testStatusControl.Clear();
			testStatusControl.Invalidate(true);
			ClearRichTextBox();
		}

		private void testStatusControl_MethodSelected(object sender, TestMethodArgs e)
		{
			ReportTests(e.Method);
		}

		private void ReportTests(IMethod m)
		{
			Cursor cursor = this.Cursor;
			try
			{
				this.Cursor = Cursors.WaitCursor;

				ClearRichTextBox();
				if (m != null)
				{
					ReportFormatter.ReportTests(m, /* proj,*/ rtCompReport);//, this.harnessitListener );
				}

				rtCompReport.Render();

				//
				// Scroll back to the top of the text box, it automatically
				// scrolls to the bottom.
				//
				rtCompReport.RichTextBox.Select( 0, 0 );
				rtCompReport.RichTextBox.ScrollToCaret();
			}
			finally
			{
				this.Cursor = cursor;
			}
		}

		private void ClearRichTextBox()
		{
			rtCompReport.Clear();
			rtCompReport.Render();
			richTextBoxDetails.Clear();
			richTextBoxDetails.Invalidate(true);
		}

		private void richTextBoxDetails_LinkClicked(object sender, LinkClickedEventArgs e)
		{
			string url = e.LinkText.Replace( '+', ' ' );

			if (!url.ToLower().StartsWith("http://exceptionexplorer/"))
			{
				return;
			}

			int start = "http://exceptionexplorer/".Length;
			string path = url.Substring(start);
			int index = path.ToLower().LastIndexOf("?id=");
			int idlength = "?id=".Length;
			int length = path.Length - idlength - index;
			string id = path.Substring(index + idlength, length);
			path = path.Substring(0, index);

			int exceptionIndex = int.Parse(id) - 1;

			Method m = Lookup.LookUpMethod(path);

			TestException TE = m.TestRecord.TestExceptions[exceptionIndex];
			if (TE.Message.IndexOf("thrown by the target of an invocation") >= 0)
			{
				TE = TE.InnerException;
			}

			ExceptionExplorerForm form = new ExceptionExplorerForm();
			form.LoadException(TE, path);
			form.ShowDialog(this);
		}

		internal void TestsStarting()
		{
			testStatusControl.TestsStarting();
		}
	}
}
